package com.hoshiibuy.spu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.spu.entity.BaseSpu;
import com.hoshiibuy.spu.feign.IdGeneratorService;
import com.hoshiibuy.spu.mapper.BaseSpuMapper;
import com.hoshiibuy.spu.service.IBaseSpuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * SPU基本资料service实现类
 *
 * @author : yangcheng
 * @date : 2019/4/30 13:58
 * @mail : maiboyc@163.com
 **/
@Service
public class BaseSpuServiceImpl extends ServiceImpl<BaseSpuMapper, BaseSpu> implements IBaseSpuService {

    // id生成service
    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
     * 根据id获取
     * @param id ID
     * @return BaseSpu
     */
    @Cacheable(cacheNames = "baseSpu", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Override
    public BaseSpu get(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 保存
     * @param entity 保存实体
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "baseSpu", allEntries = true)
    @Override
    public boolean save(BaseSpu entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (id == null) {
            throw new ServiceException(CommonError.error("新增spu基本资料失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        entity.setUpdatedtime(LocalDateTime.now());
        //需要做日期转换
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增spu基本资料失败"));
        }
        return true;
    }

    /**
     * 修改
     * @param entity 更新实体
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "baseSpu", allEntries = true)
    @Override
    public boolean update(BaseSpu entity) {
        entity.setUpdatedtime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改spu基本资料失败"));
        }
        return true;
    }

    /**
     * 查询
     * @param entity 查询参数实体
     * @return IPage<GoodsSpu>
     */
    @CacheEvict(cacheNames = "baseSpu", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#entity.pageIndex")
    @Override
    public IPage<BaseSpu> List(BaseSpu entity) {
        QueryWrapper<BaseSpu> queryWrapper = new QueryWrapper();
        IPage<BaseSpu> page = new Page();
        if(entity.getPageIndex() != null) {
            page.setCurrent(entity.getPageIndex());
        }
        if(entity.getPageSize() != null){
            page.setSize(entity.getPageSize());
        }
        if (StringUtils.isNotEmpty(entity.getTitle())) {
            queryWrapper.like("title", entity.getTitle());
        }
        if(StringUtils.isNotEmpty(entity.getSubTitle())) {
            queryWrapper.like("sub_title", entity.getSubTitle());
        }
        if(StringUtils.isNotEmpty(entity.getSaleUnit())) {
            queryWrapper.like("sale_unit", entity.getSaleUnit());
        }
        if(StringUtils.isNotEmpty(entity.getPacketUnit())) {
            queryWrapper.like("packet_unit",entity.getPacketUnit());
        }
        return baseMapper.selectPage(page,queryWrapper);
    }

    /**
     * 删除
     * @param id ID
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "baseSpu", allEntries = true)
    @Override
    public boolean delete(Long id) {
        int row = baseMapper.deleteById(id);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除spu基本资料失败"));
        } else {
            return true;
        }
    }

}